home *** CD-ROM | disk | FTP | other *** search
- Path: cscsun3.larc.nasa.gov!hook
- From: hook@cscsun3.larc.nasa.gov (Ed Hook)
- Newsgroups: comp.lang.c
- Subject: Re: Q: realloc->free?
- Date: 16 Jan 1996 13:34:50 GMT
- Organization: CSC/NASA Langley Research Center
- Distribution: world
- Message-ID: <4dg9hq$fjn@reznor.larc.nasa.gov>
- References: <4daa2e$oh5@axe.netdoor.com> <4df2ud$706@oxy.rust.net>
- Reply-To: hook@cscsun3.larc.nasa.gov
- NNTP-Posting-Host: cscsun3.larc.nasa.gov
-
- In article <4df2ud$706@oxy.rust.net>, ebennett@rust.net writes:
- |> esargent@netdoor.com (Eric Sargent) wrote:
- |>
- |>
- |>
- |> > Now let's say realloc had to move the data so a != b. Does realloc
- |> >free the memory previously pointed to by a or should it be explicitly
- |> >freed if realloc returns a new location? I checked the FAQ, but there
- |> >was nothing specific about realloc. Thanks for any information.
- |>
- |> realloc() will free the old block. It is perfectly legal to say
- |>
- |> a = realloc(a, newsize);
- |>
- Yes -- this _is_ perfectly legal.
-
- |> No memory loss should occur from this.
- |>
- Nope -- 'realloc()' can fail, a situation that it communicates by
- returning NULL. If this happens, you've leaked the memory to which
- 'a' pointed. The *safe* approach is somewhat more anal-retentive:
-
- tmp_ptr = realloc(a,newsize);
- if ( tmp_ptr )
- a = tmp_ptr;
- else {
- /* deal with the situation -- 'a' is still valid */
- }
-
- |> Earl
- |>
- |>
-
- --
- Ed Hook | Coppula eam, se non posit
- Computer Sciences Corporation | acceptera jocularum.
- NASA Langley Research Center | Me? Speak for my employer?...<*snort*>
- Internet: hook@cscsun3.larc.nasa.gov | ... Get a _clue_ !!! ...
-